// Spike.js for PSPTube
// Made By JK108

function Spike_CheckURL( url, option )
{
	if (url.match("http://www.spike.com") == "http://www.spike.com"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a Spike URL
*/
function Spike_GetURL( url, option )
{
	var contents = GetContents("http://keepvid.com/?url=" + url);
	if(contents == null) {return null; /* failed to get page */}
	var video_url = contents.match(/\<\/script\>\<br \/\>\<br \/\>\<a href\=\"(.*?)\"/);
	var video_url = video_url[1];
	return video_url;
}

function Spike_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;
	var perpage = 10;


	result.start = (((result.start - 1) / 10) * perpage) + 1;


	if(result.start < perpage){
	var contents = GetContents("http://www.spike.com/search?query=" + keyword.replace(/ /g, "%20") + "&tabName=video&allNo=8878");
	} else{
	var num = 1 + ((result.start - 1) / perpage);
	var contents = GetContents("http://www.spike.com/search?query=" + keyword.replace(/ /g, "+") + "&tabName=video&videoNo=8146&page=" + num + "&numPerPage=10");
	}
	if(contents == null){return null; /* failed to get page */}


	if(contents.match("</b> - did not match any video.") == "</b> - did not match any video."){result.total = 0; return result;}
	var pos = contents.indexOf("<li id=\"result_count\">");
	contents = contents.substring(pos + 22, contents.length);
	var total = contents.match(/ of (.*?)\n/);
	var total = total[1].replace(/,/g, "");
	result.total = total;


	while(1){
	pos = contents.indexOf("<div class=\"title_date\">");

	if(pos != -1){
		contents = contents.substring(pos + 24, contents.length);
		//Get URL
		var url = contents.match(/\<a href\=\"(.*?)\"/);
		var url = "http://www.spike.com" + url[1];
		//Get Thumbnail
		var thumb_url = contents.match(/\<img src\=\"(.*?)\"/);
		var thumb_url = thumb_url[1];
		//Title
		var title = contents.match(/title\=\"(.*?)\"/);
		var title = ImproveText(title[1]);
		//Length
		var haslength = contents.match(/\<li\>(.*?)\<\/li\>/);
		var haslength = haslength[1].substring(haslength[1].length - 5, haslength[1].length);
		if(haslength == "Views"){
			var length = 0;
		} else{
			var length = contents.match(/\<li\>\<strong\>(.*?)\<\/strong\>\<\/li\>/);
			if(length[1].substring(0, 1) == ":"){length[1] = "00" + length[1];}
			var length = TimestringToSeconds(length[1]);
		}
		//Description
		pos = contents.indexOf("<p>");
		var description = contents.substring(pos + 36, contents.length);
		pos = description.indexOf("\n");
		description = splitTo2Lines(ImproveText(description.substring(0, pos)), 60);
		//Views
		if(haslength != "Views"){
			pos = contents.indexOf("<li><strong>");
			var views = contents.substring(pos + 12, contents.length);
			var views = views.match(/\<li\>\<strong\>(.*?)\<\/strong\>/);
		} else{
			var views = contents.match(/\<li\>\<strong\>(.*?)\<\/strong\>/);
		}
		var views = parseInt(views[1].replace(/,/g, ""));
		//Rating
		pos = contents.indexOf("<li><strong>");
		var rating = contents.substring(pos + 12, contents.length);
		if(haslength != "Views"){
			pos = rating.indexOf("<li><strong>");
			var rating = rating.substring(pos + 12, contents.length);
		}
		var rating = rating.match(/\<li\>\<strong\>(.*?)\<\/strong\>/);
		var rating = parseFloat(rating[1]);
		//Comments
		var comments = contents.match(/\<li\>\<a href\=\"(.*?)\<\/a\>/);
		var comments = comments[1].match(/\"\>(.*?) Comments/);
		var comments = parseInt(comments[1].replace(/,/g, ""));

		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.LengthSeconds = length;
		info.Description = description;
		info.ViewCount = views;
		info.RatingAvg = rating;
		info.CommentCount = comments;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.VideoInfo.length == perpage){break;}
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != perpage){result.total = result.end;}

	return result;
}

// Service metadata
var Spike = new Object();
Spike.Name          = "Spike";		/* Service name */
Spike.Description   = "Spike";		/* Service description */
Spike.SearchDesc    = "Spike";		/* Service search screen description */
Spike.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
Spike.CheckURL      = Spike_CheckURL;	/* CheckURL function */
Spike.GetURL        = Spike_GetURL;		/* GetURL function */
Spike.Search        = Spike_Search;		/* Search function */

// add Spike to site list
SiteList.push( Spike );
